package com.xintonghua.bussiness.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.xintonghua.base.adapter.BaseAdapter;
import com.xintonghua.base.widget.ShowAllListView;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.bean.ServiceItems;
import com.xintonghua.bussiness.bean.ShopOrderListBean;
import com.xintonghua.bussiness.ui.home.AddLogActivity;
import com.xintonghua.bussiness.util.MyUtils;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liqi on 2018/3/30.
 */

public class SaveModeToListAdapter extends BaseAdapter<ShopOrderListBean.ListBean, SaveModeToListAdapter.OrderViewHolder> {

    public SaveModeToListAdapter(Activity mActivity, List<ShopOrderListBean.ListBean> list) {
        super(mActivity, list);
    }

    @Override
    public SaveModeToListAdapter.OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_home_order, parent, false);
        return new SaveModeToListAdapter.OrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final SaveModeToListAdapter.OrderViewHolder holder, int position) {
        final ShopOrderListBean.ListBean bean = getList().get(position);
        holder.tvName.setText(bean.getNickname());
        holder.tvPhone.setText(bean.getPhone());
        holder.tvTime.setText(bean.getSubscribe_time());
        holder.tvPayMoney.setText(MyUtils.getMoney(mActivity, bean.getConsumption()));
        final List<ServiceItems> itemsList;
        if (bean.getServiceItems().size() > 1) {
            itemsList = bean.getServiceItems().subList(0, 1);
        } else {
            itemsList = bean.getServiceItems();
        }
        holder.lvGoods.setAdapter(new GoodsAdapter(itemsList, mActivity));
        //  getStatus    -1:取消,1:预约,2:到店,3:已完成
        switch (bean.getStatus()) {
            case -1:
                holder.tvSureStore.setVisibility(View.VISIBLE);
                holder.tvOrderCancle.setText("取消订单");
                holder.tvOrderCancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                break;
            case 1:
                holder.tvSureStore.setVisibility(View.VISIBLE);
                holder.tvOrderCancle.setText("预约");
                holder.tvOrderCancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                break;
            case 2:
                holder.tvOrderCancle.setVisibility(View.GONE);
                holder.tvSureStore.setText("添加日志");
                holder.tvSureStore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putInt(AddLogActivity.ORDER_ID, bean.getId());
                        bundle.putInt(AddLogActivity.USE_TYPE, 2);
                        bundle.putSerializable(AddLogActivity.USER_ITEM_DATA, (Serializable) bean);
                        MyUtils.openActivity(mActivity, AddLogActivity.class, bundle);
                    }
                });
                break;
            case 3:
                holder.tvSureStore.setVisibility(View.GONE);
                holder.tvOrderCancle.setText("查看日志");
                holder.tvOrderCancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putInt(AddLogActivity.ORDER_ID, bean.getId());
                        bundle.putInt(AddLogActivity.USE_TYPE, 1);
                        MyUtils.openActivity(mActivity, AddLogActivity.class, bundle);
                    }
                });
                break;
        }
        holder.lvGoods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                holder.lvGoods.setAdapter(new GoodsAdapter(bean.getServiceItems(), mActivity));
            }
        });

    }

    @Override
    public int getItemCount() {
        return getList().size();
    }

    class OrderViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_user_head)
        ImageView ivUserHead;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_time_tag)
        TextView tvTimeTag;
        @BindView(R.id.lv_goods)
        ShowAllListView lvGoods;
        @BindView(R.id.tv_order_cancle)
        TextView tvOrderCancle;
        @BindView(R.id.tv_sure_store)
        TextView tvSureStore;
        @BindView(R.id.tv_phone)
        TextView tvPhone;
        @BindView(R.id.tv_pay_money)
        TextView tvPayMoney;


        public OrderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}